package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.DestinationQuery;
import cn.wolfcode.luowowo.query.RegionQuery;
import cn.wolfcode.luowowo.repository.DestinationRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.DBHelper;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository repository;
    @Autowired
    private IRegionService regionService;

    @Override
    public Destination get(String id) {
        Optional<Destination> byId = repository.findById(id);
        return byId.get();
    }

    @Override
    public List<Destination> list() {
        return repository.findAll();
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        Region region = regionService.get(rid);
        List<String> refIds = region.getRefIds();
        //目的地集合
        return repository.findByIdIn(refIds);
    }

    @Autowired
    private MongoTemplate template;

    /**
     *
     * @param qo
     * @param parentId 例如广东下的页面--parentId就是广东的id
     * @return
     */
    @Override
    public Page list(DestinationQuery qo,String parentId) {
        Query query = new Query();
        //分级查询  例如中国>>广东>>广州
        if (StringUtils.hasLength(parentId)){
            query.addCriteria(Criteria.where("parentId").is(parentId));
        }
        //高级查询的时候添加查询条件
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(".*" + qo.getKeyword() + ".*"));
        }
        Page<Destination> page = DBHelper.query(template, Destination.class, query, qo.getPageable());
        return page;
    }

    @Override
    public void save(String info,String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("info",info);
        template.updateMulti(query,update,Destination.class);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public List<Destination> getToasts(String parentId) {
        if (!StringUtils.hasLength(parentId)){
            return Collections.emptyList();
        }
        //新创建一个集合,存放吐司的名字
        List<Destination> list = new ArrayList();
        //利用递归查询到parentId是空的就停止循环
        createTosts(list,parentId);
        //加入的数据是反的,纠正过来
        Collections.reverse(list);
        return list;
    }



    private void createTosts(List<Destination> list, String parentId) {
        Optional<Destination> optional = repository.findById(parentId);
        if (!optional.isPresent()){
            return;
        }
        list.add(optional.get());
        //如果当前的对象还有parentId的话就继续循环 一直找到中国这一级
        if (StringUtils.hasLength(optional.get().getParentId())){
            createTosts(list,optional.get().getParentId());
        }
    }

    @Override
    public List<Destination> getDestByRegionIdForApi(String regionId) {
        List<Destination> list = null;
        //中国是初始化数据  直接传的-1
        if ("-1".equals(regionId)){
            list = repository.findByParentName("中国");
        }else {
            Region region = regionService.get(regionId);
            //获取区域下的子地区的id
            List<String> refIds = region.getRefIds();
            //通过子id的数组  查到封装起来的各个国家
            list =  repository.findByIdIn(refIds);
        }
        //查询下面的字目的地  比如广东省的各个城市
        for (Destination destination : list) {
            //只显示5个出来
            PageRequest of = PageRequest.of(0, 5);
            List<Destination> children = repository.findByParentId(destination.getId(),of);
            //数据的优化
            destination.setChildren(children);
        }
        return list;
    }

    @Override
    public Destination getByName(String keyword) {

        return repository.findByName(keyword);
    }
}
